package com.smartleanx.module.hrm.service.salary;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.tenant.core.aop.TenantIgnore;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.esign.ContractPackage;
import com.smartleanx.module.hrm.dal.dataobject.esign.SystemConfig;
import com.smartleanx.module.hrm.dal.dataobject.salary.*;
import com.smartleanx.module.hrm.dal.dataobject.salary.bo.HrmSalaryPlanInsertBo;
import com.smartleanx.module.hrm.dal.dataobject.salary.vo.HrmSalaryPlanInsertVo;
import com.smartleanx.module.hrm.dal.dataobject.salary.vo.HrmSalaryTableConfigVo;
import com.smartleanx.module.hrm.dal.dataobject.salary.vo.HrmSalaryTaxationRuleVo;
import com.smartleanx.module.hrm.dal.dataobject.salary.vo.SystemColumnTenantVo;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.ContractPackageMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.SystemConfigMapper;
import com.smartleanx.module.hrm.dal.mysql.salary.*;
import com.smartleanx.module.hrm.service.examine.util.DateUtil;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author guozhenpeng
 * @since 2024-12-23
 */
@Service
public class HrmSalaryTableConfigServiceImpl implements HrmSalaryTableConfigService {


    @Autowired
    private HrmSalaryTableConfigMapper hrmSalaryTableConfigMapper;
    @Autowired
    private HrmSalaryTableConfigTenantMapper hrmSalaryTableConfigTenantMapper;
    @Autowired
    private HrmSalarySettingMapper salarySettingMapper;
    @Autowired
    private HrmSalaryTaxationRuleTenantMapper salaryTaxationRuleTenantMapper;
    @Autowired
    private HrmSalaryPlanMapper salaryPlanMapper;
    @Autowired
    private HrmSalaryPlanEmployeeMapper salaryPlanEmployeeMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private HrmSalaryPlanEmployeeMapper hrmSalaryPlanEmployeeMapper;

    @Autowired
    private SystemColumnTenantMapper systemColumnTenantMapper;

    @Autowired
    private SystemConfigMapper configMapper;
    @Autowired
    private ContractPackageMapper contractPackageMapper;
    @Autowired
    private SystemConfigMapper systemConfigMapper;

    @Override
    public List<HrmSalaryTableConfigVo> selectList(Integer type) {
        List<HrmSalaryTableConfigVo> salaryTableConfigVoList = new ArrayList<>();
        List<HrmSalaryTableConfigTenant> hrmSalaryTableConfigs = hrmSalaryTableConfigTenantMapper.selectList(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
            eq(HrmSalaryTableConfigTenant::getType, type);
            eq(HrmSalaryTableConfigTenant::getPid, 0);
            eq(HrmSalaryTableConfigTenant::getIsDisplayed, 1);
            orderByAsc(HrmSalaryTableConfigTenant::getTableOrder);
        }});
        if (!ObjectUtils.isEmpty(hrmSalaryTableConfigs)) {
            hrmSalaryTableConfigs.forEach(hrmSalaryTableConfig -> {

                HrmSalaryTableConfigVo hrmSalaryTableConfigVo = new HrmSalaryTableConfigVo();
                BeanUtils.copyProperties(hrmSalaryTableConfig, hrmSalaryTableConfigVo);
//                hrmSalaryTableConfigVo.setIsTaxation("是否计税");
                hrmSalaryTableConfigVo.setIsAddSubtract("加/减项");
                List<HrmSalaryTableConfigTenant> hrmSalaryTableConfigSons = hrmSalaryTableConfigTenantMapper.selectList(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
                    eq(HrmSalaryTableConfigTenant::getPid, hrmSalaryTableConfig.getId());
                    eq(HrmSalaryTableConfigTenant::getIsDisplayed, 1);
                    orderByAsc(HrmSalaryTableConfigTenant::getTableOrder);
                }});
                if (!ObjectUtils.isEmpty(hrmSalaryTableConfigSons)) {
                    List<HrmSalaryTableConfigVo> hrmSalaryTableConfigVoSons = new ArrayList<>();
                    hrmSalaryTableConfigSons.forEach(hrmSalaryTableConfigSon -> {
                        HrmSalaryTableConfigVo hrmSalaryTableConfigVo1 = new HrmSalaryTableConfigVo();
                        BeanUtils.copyProperties(hrmSalaryTableConfigSon, hrmSalaryTableConfigVo1);
//                        hrmSalaryTableConfigVo1.setIsTaxation(hrmSalaryTableConfig.getIsTaxation() + "");
                        hrmSalaryTableConfigVo1.setIsAddSubtract(hrmSalaryTableConfig.getIsAddSubtract() + "");
                        hrmSalaryTableConfigVoSons.add(hrmSalaryTableConfigVo1);
                    });
                    hrmSalaryTableConfigVo.setChildren(hrmSalaryTableConfigVoSons);
                } else {
                    hrmSalaryTableConfigVo.setChildren(new ArrayList<>());
                }
                salaryTableConfigVoList.add(hrmSalaryTableConfigVo);
            });
        }
        return salaryTableConfigVoList;
    }


    @Override
    public List<HrmSalaryTableConfigVo> selectListInfo(Integer type) {
        List<HrmSalaryTableConfigVo> salaryTableConfigVoList = new ArrayList<>();
        List<HrmSalaryTableConfigTenant> hrmSalaryTableConfigs = hrmSalaryTableConfigTenantMapper.selectList(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
            eq(HrmSalaryTableConfigTenant::getType, type);
            eq(HrmSalaryTableConfigTenant::getPid, 0);
            eq(HrmSalaryTableConfigTenant::getStatus, 1);
            eq(HrmSalaryTableConfigTenant::getIsDisplayed, 1);
        }});
        if (!ObjectUtils.isEmpty(hrmSalaryTableConfigs)) {
            hrmSalaryTableConfigs.forEach(hrmSalaryTableConfig -> {
                HrmSalaryTableConfigVo hrmSalaryTableConfigVo = new HrmSalaryTableConfigVo();
                BeanUtils.copyProperties(hrmSalaryTableConfig, hrmSalaryTableConfigVo);
//                hrmSalaryTableConfigVo.setIsTaxation("是否计税");
                hrmSalaryTableConfigVo.setIsAddSubtract("加/减项");
                List<HrmSalaryTableConfigTenant> hrmSalaryTableConfigSons = hrmSalaryTableConfigTenantMapper.selectList(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
                    eq(HrmSalaryTableConfigTenant::getPid, hrmSalaryTableConfig.getId());
                    eq(HrmSalaryTableConfigTenant::getIsDisplayed, 1);
                }});
                if (!ObjectUtils.isEmpty(hrmSalaryTableConfigSons)) {
                    List<HrmSalaryTableConfigVo> hrmSalaryTableConfigVoSons = new ArrayList<>();
                    hrmSalaryTableConfigSons.forEach(hrmSalaryTableConfigSon -> {
                        HrmSalaryTableConfigVo hrmSalaryTableConfigVo1 = new HrmSalaryTableConfigVo();
                        BeanUtils.copyProperties(hrmSalaryTableConfigSon, hrmSalaryTableConfigVo1);
                        hrmSalaryTableConfigVo1.setIsAddSubtract(hrmSalaryTableConfig.getIsAddSubtract() + "");
                        hrmSalaryTableConfigVoSons.add(hrmSalaryTableConfigVo1);
                    });
                    hrmSalaryTableConfigVo.setChildren(hrmSalaryTableConfigVoSons);
                } else {
                    hrmSalaryTableConfigVo.setChildren(new ArrayList<>());
                }
                salaryTableConfigVoList.add(hrmSalaryTableConfigVo);
            });
        }
        return salaryTableConfigVoList;
    }


    @Override
    public List<SystemColumnTenantVo> selectColumnListInfo(Integer type) {
        List<SystemColumnTenantVo> salaryTableConfigVoList = new ArrayList<>();
        List<SystemColumnTenant> hrmSalaryTableConfigs = systemColumnTenantMapper.selectList(new LambdaQueryWrapper<SystemColumnTenant>() {{
            eq(SystemColumnTenant::getConfigType, type);
            eq(SystemColumnTenant::getParentId, 0);
        }});
        if (!ObjectUtils.isEmpty(hrmSalaryTableConfigs)) {
            hrmSalaryTableConfigs.forEach(hrmSalaryTableConfig -> {
                SystemColumnTenantVo hrmSalaryTableConfigVo = new SystemColumnTenantVo();
                BeanUtils.copyProperties(hrmSalaryTableConfig, hrmSalaryTableConfigVo);
                List<SystemColumnTenant> hrmSalaryTableConfigSons = systemColumnTenantMapper.selectList(new LambdaQueryWrapper<SystemColumnTenant>() {{
                    eq(SystemColumnTenant::getParentId, hrmSalaryTableConfig.getId());
                }});
                if (!ObjectUtils.isEmpty(hrmSalaryTableConfigSons)) {
                    List<SystemColumnTenantVo> hrmSalaryTableConfigVoSons = new ArrayList<>();
                    hrmSalaryTableConfigSons.forEach(hrmSalaryTableConfigSon -> {
                        SystemColumnTenantVo hrmSalaryTableConfigVo1 = new SystemColumnTenantVo();
                        BeanUtils.copyProperties(hrmSalaryTableConfigSon, hrmSalaryTableConfigVo1);

                        hrmSalaryTableConfigVoSons.add(hrmSalaryTableConfigVo1);
                    });
                    hrmSalaryTableConfigVo.setChildren(hrmSalaryTableConfigVoSons);
                } else {
                    hrmSalaryTableConfigVo.setChildren(new ArrayList<>());
                }
                salaryTableConfigVoList.add(hrmSalaryTableConfigVo);
            });
        }
        return salaryTableConfigVoList;
    }


    @Override
    public Integer columnInsertOrUpdate(List<SystemColumnTenantVo> systemColumnTenantVos) {
        if (ObjectUtils.isEmpty(systemColumnTenantVos)) {
            throw new ResultException("薪资设置项不可以为空");
        }
        for (SystemColumnTenantVo systemColumnTenantVo : systemColumnTenantVos) {
            SystemColumnTenant systemColumnTenant = new SystemColumnTenant();
            BeanUtils.copyProperties(systemColumnTenantVo, systemColumnTenant);
            systemColumnTenant.setStatus(systemColumnTenantVo.getStatus());
            if (systemColumnTenantVo.getStatus() == 0) {
                List<SystemColumnTenant> hrmSalaryTableConfigTenants = systemColumnTenantMapper.selectList(new LambdaQueryWrapper<SystemColumnTenant>() {{
                    eq(SystemColumnTenant::getParentId, systemColumnTenantVo.getId());
                }});
                hrmSalaryTableConfigTenants.forEach(hrmSalaryTableConfigTenant -> {
                    hrmSalaryTableConfigTenant.setStatus(systemColumnTenantVo.getStatus());
                    systemColumnTenantMapper.updateById(hrmSalaryTableConfigTenant);
                });
            }
            systemColumnTenantMapper.updateById(systemColumnTenant);
//            if (ObjectUtils.isEmpty(systemColumnTenantVo.getChildren())) {
//                continue;
//            }
            List<SystemColumnTenantVo> children = systemColumnTenantVo.getChildren();
            //获取存在重复的数据
            List<Map.Entry<String, List<SystemColumnTenantVo>>> collect = children.stream().collect(Collectors.groupingBy(SystemColumnTenantVo::getName))
                    .entrySet().stream().filter(e -> e.getValue().size() > 1).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(collect)) {
                //name
                List<String> collect2 = collect.stream().map(Map.Entry::getKey).collect(Collectors.toList());
                for (int i = 0; i < collect2.size(); i++) {
                    System.out.println("名称:" + collect2.get(i));
                }
                throw new ResultException(systemColumnTenant.getName() + "下有重复名称数据");
            } else {
                List<SystemColumnTenantVo> collect1 = children.stream().filter(systemColumnTenantVo1 -> ObjectUtils.isEmpty(systemColumnTenantVo1.getId())).collect(Collectors.toList());
                List<SystemColumnTenantVo> collect3 = children.stream().filter(systemColumnTenantVo1 -> !ObjectUtils.isEmpty(systemColumnTenantVo1.getId())).collect(Collectors.toList());
                systemColumnTenantMapper.delete(new LambdaQueryWrapper<SystemColumnTenant>() {{
                    eq(SystemColumnTenant::getParentId, systemColumnTenantVo.getId());
                    notIn(!ObjectUtils.isEmpty(collect3), SystemColumnTenant::getId, collect3.stream().map(SystemColumnTenantVo::getId).collect(Collectors.toList()));
                }});
//                System.err.println(SystemColumnTenant::getId,collect3.stream().map(SystemColumnTenantVo::getId).collect(Collectors.joining(","))+"");
                if (!ObjectUtils.isEmpty(collect1)) {
                    for (SystemColumnTenantVo systemColumnTenantVo1 : collect1) {
                        SystemColumnTenant hrmSalaryTableConfigTenant = systemColumnTenantMapper.selectOne(new LambdaQueryWrapper<SystemColumnTenant>() {{
                            eq(SystemColumnTenant::getName, systemColumnTenantVo1.getName());
                            last("for update");
                        }});

                        if (ObjectUtils.isEmpty(hrmSalaryTableConfigTenant)) {
                            systemColumnTenant = new SystemColumnTenant();
                            systemColumnTenant.setParentId(systemColumnTenantVo.getId());
                            systemColumnTenant.setName(systemColumnTenantVo1.getName());
                            systemColumnTenant.setStatus(1);
                            systemColumnTenant.setIsBuilt(0);
                            systemColumnTenant.setConfigType(2);
                            systemColumnTenant.setColumnType(0);
                            systemColumnTenant.setIsGence(0);
                            systemColumnTenant.setType(2);
                            systemColumnTenantMapper.insert(systemColumnTenant);
                        }
                    }
                }
                List<SystemColumnTenantVo> collect2 = children.stream().filter(systemColumnTenantVo1 -> !ObjectUtils.isEmpty(systemColumnTenantVo1.getId())).collect(Collectors.toList());
                if (!ObjectUtils.isEmpty(collect2)) {
                    for (SystemColumnTenantVo systemColumnTenantVo1 : collect2) {
                        SystemColumnTenant systemColumnTenantUpdate = systemColumnTenantMapper.selectOne(new LambdaQueryWrapper<SystemColumnTenant>() {{
                            eq(SystemColumnTenant::getName, systemColumnTenantVo1.getName());
                            eq(SystemColumnTenant::getParentId, systemColumnTenantVo1.getParentId());
                            ne(SystemColumnTenant::getId, systemColumnTenantVo1.getId());
                        }});
                        if (ObjectUtils.isEmpty(systemColumnTenantUpdate)) {
                            systemColumnTenantUpdate = new SystemColumnTenant();
                            systemColumnTenantUpdate.setId(systemColumnTenantVo1.getId());
                            systemColumnTenantUpdate.setName(systemColumnTenantVo1.getName());
                            systemColumnTenantUpdate.setStatus(systemColumnTenantVo1.getStatus());
                            systemColumnTenantMapper.updateById(systemColumnTenantUpdate);
                        } else {
                            throw new ResultException(systemColumnTenantVo.getName() + "在" + systemColumnTenantVo.getName() + "下已存在");
                        }
                    }
                }
            }
        }
        return 0;
    }


    @Override
    public Integer insertOrUpdate(List<HrmSalaryTableConfigVo> hrmSalaryTableConfigVos) {
        if (ObjectUtils.isEmpty(hrmSalaryTableConfigVos)) {
            throw new ResultException("薪资设置项不可以为空");
        }
        for (HrmSalaryTableConfigVo hrmSalaryTableConfigVo : hrmSalaryTableConfigVos) {
            HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant11 = new HrmSalaryTableConfigTenant();
            BeanUtils.copyProperties(hrmSalaryTableConfigVo, hrmSalaryTableConfigTenant11);
            hrmSalaryTableConfigTenant11.setStatus(hrmSalaryTableConfigVo.getStatus());
            hrmSalaryTableConfigTenantMapper.updateById(hrmSalaryTableConfigTenant11);
            if (ObjectUtils.isEmpty(hrmSalaryTableConfigVo.getChildren())) {
                continue;
            }
            List<HrmSalaryTableConfigVo> children = hrmSalaryTableConfigVo.getChildren();
            //获取存在重复的数据
            List<Map.Entry<String, List<HrmSalaryTableConfigVo>>> collect = children.stream().collect(Collectors.groupingBy(HrmSalaryTableConfigVo::getName))
                    .entrySet().stream().filter(e -> e.getValue().size() > 1).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(collect)) {
                //name
                List<String> collect2 = collect.stream().map(Map.Entry::getKey).collect(Collectors.toList());
                for (int i = 0; i < collect2.size(); i++) {
                    System.out.println("名称:" + collect2.get(i));
                }
                throw new ResultException(hrmSalaryTableConfigVo.getName() + "下有重复名称数据");
            } else {
                List<Integer> collect4 = new ArrayList<>();
                List<HrmSalaryTableConfigVo> collect1 = children.stream().filter(salaryTableConfigVo -> ObjectUtils.isEmpty(salaryTableConfigVo.getId())).collect(Collectors.toList());
                if (!ObjectUtils.isEmpty(collect1)) {

                    for (HrmSalaryTableConfigVo salaryTableConfigVo : collect1) {
                        HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = hrmSalaryTableConfigTenantMapper.selectOne(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
                            eq(HrmSalaryTableConfigTenant::getName, salaryTableConfigVo.getName());
                            last("for update");
                        }});
                        if (ObjectUtils.isEmpty(hrmSalaryTableConfigTenant)) {
                            hrmSalaryTableConfigTenant = new HrmSalaryTableConfigTenant();
                            hrmSalaryTableConfigTenant.setPid(hrmSalaryTableConfigVo.getId());
                            hrmSalaryTableConfigTenant.setName(salaryTableConfigVo.getName());
                            hrmSalaryTableConfigTenant.setType(2);
                            hrmSalaryTableConfigTenant.setStatus(1);
                            hrmSalaryTableConfigTenant.setIsUpdate(0);
                            hrmSalaryTableConfigTenant.setIsNameUpdate(0);
//                          hrmSalaryTableConfigTenant.setIsTaxation(Integer.parseInt(salaryTableConfigVo.getIsTaxation()));
                            hrmSalaryTableConfigTenant.setIsDisplayed(1);
                            hrmSalaryTableConfigTenant.setIsAddSubtract(Integer.parseInt(salaryTableConfigVo.getIsAddSubtract()));
                            hrmSalaryTableConfigTenant.setRemark(salaryTableConfigVo.getRemark());
                            hrmSalaryTableConfigTenantMapper.insert(hrmSalaryTableConfigTenant);
                        } else {
                            if (hrmSalaryTableConfigTenant.getIsDisplayed() == 0 && hrmSalaryTableConfigTenant.getRemark().equals(salaryTableConfigVo.getRemark())) {
                                hrmSalaryTableConfigTenant.setIsDisplayed(1);
                                hrmSalaryTableConfigTenant.setIsAddSubtract(Integer.parseInt(salaryTableConfigVo.getIsAddSubtract()));
                                hrmSalaryTableConfigTenant.setRemark(salaryTableConfigVo.getRemark());
                                hrmSalaryTableConfigTenant.setStatus(salaryTableConfigVo.getStatus());
                                hrmSalaryTableConfigTenantMapper.updateById(hrmSalaryTableConfigTenant);

                                collect4.add(hrmSalaryTableConfigTenant.getId());
                            } else {
                                throw new ResultException(salaryTableConfigVo.getName() + "在" + hrmSalaryTableConfigVo.getName() + "下已存在");
                            }
                        }
                    }
                }
                List<HrmSalaryTableConfigVo> collect2 = children.stream().filter(salaryTableConfigVo -> !ObjectUtils.isEmpty(salaryTableConfigVo.getId())).collect(Collectors.toList());
                if (!ObjectUtils.isEmpty(collect2)) {
                    for (HrmSalaryTableConfigVo salaryTableConfigVo : collect2) {
                        HrmSalaryTableConfigTenant hrmSalaryTableConfigTenantUpdate = hrmSalaryTableConfigTenantMapper.selectOne(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
                            eq(HrmSalaryTableConfigTenant::getName, salaryTableConfigVo.getName());
                            eq(HrmSalaryTableConfigTenant::getPid, salaryTableConfigVo.getPid());
                            ne(HrmSalaryTableConfigTenant::getId, salaryTableConfigVo.getId());

                        }});
                        if (ObjectUtils.isEmpty(hrmSalaryTableConfigTenantUpdate)) {
                            hrmSalaryTableConfigTenantUpdate = new HrmSalaryTableConfigTenant();
                            hrmSalaryTableConfigTenantUpdate.setId(salaryTableConfigVo.getId());
                            hrmSalaryTableConfigTenantUpdate.setName(salaryTableConfigVo.getName());
                            hrmSalaryTableConfigTenantUpdate.setRemark(salaryTableConfigVo.getRemark());
                            hrmSalaryTableConfigTenantUpdate.setIsAddSubtract(Integer.parseInt(salaryTableConfigVo.getIsAddSubtract()));
                            hrmSalaryTableConfigTenantUpdate.setStatus(salaryTableConfigVo.getStatus());
                            hrmSalaryTableConfigTenantMapper.updateById(hrmSalaryTableConfigTenantUpdate);
                        } else {
                            throw new ResultException(salaryTableConfigVo.getName() + "在" + hrmSalaryTableConfigVo.getName() + "下已存在");
                        }
                    }
                    List<HrmSalaryTableConfigTenant> salaryTableConfigTenants = hrmSalaryTableConfigTenantMapper.selectList(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
                        eq(HrmSalaryTableConfigTenant::getPid, hrmSalaryTableConfigVo.getId());
                        eq(HrmSalaryTableConfigTenant::getIsDisplayed, 1);
                    }});
                    if (!ObjectUtils.isEmpty(salaryTableConfigTenants)) {
                        collect4 = collect2.stream().map(HrmSalaryTableConfigVo::getId).collect(Collectors.toList());
                        List<Integer> collect3 = salaryTableConfigTenants.stream().map(HrmSalaryTableConfigTenant::getId).collect(Collectors.toList());
                        List<Integer> finalCollect = collect4;
                        List<Integer> collect5 = collect3.stream().filter(i -> !finalCollect.contains(i)).collect(Collectors.toList());
                        if (!ObjectUtils.isEmpty(collect5) && collect5.size() > 0) {
                            List<HrmSalaryTableConfigTenant> hrmSalaryTableConfigTenants = hrmSalaryTableConfigTenantMapper.selectList(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
                                in(HrmSalaryTableConfigTenant::getId, collect5);
                            }});
                            hrmSalaryTableConfigTenants.forEach(hrmSalaryTableConfigTenant -> {
                                hrmSalaryTableConfigTenant.setIsDisplayed(1);
                                hrmSalaryTableConfigTenantMapper.updateById(hrmSalaryTableConfigTenant);
                            });
                        }
                    }
                }
            }
        }
        return 0;
    }


    @Override
    public Integer salarySettingSaveOrUpdate(Integer type) {
        HrmSalarySetting salarySetting = salarySettingMapper.selectOne(null);
        if (ObjectUtils.isEmpty(salarySetting)) {
            salarySetting = new HrmSalarySetting();
            salarySetting.setType(type);
            salarySettingMapper.insert(salarySetting);
        } else {
            salarySetting.setType(type);
            salarySettingMapper.updateById(salarySetting);
        }

        return 0;
    }


    @Override
    public Integer selectSetting() {
        HrmSalarySetting salarySetting = salarySettingMapper.selectOne(null);

        if (ObjectUtils.isEmpty(salarySetting)) {
            return null;
        }
        return salarySetting.getType();
    }


    @Override
    public Integer init(Integer tenantId) {
        List<HrmSalaryTableConfigTenant> hrmSalaryTableConfigs = hrmSalaryTableConfigTenantMapper.selectList(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {{
            eq(HrmSalaryTableConfigTenant::getTenantId, 18);
            orderByAsc(HrmSalaryTableConfigTenant::getId);
        }});
        hrmSalaryTableConfigs.forEach(hrmSalaryTableConfig -> {
            Integer pid = 0;
            HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = new HrmSalaryTableConfigTenant();
            hrmSalaryTableConfig.setId(null);
            BeanUtils.copyProperties(hrmSalaryTableConfig, hrmSalaryTableConfigTenant);
            if (hrmSalaryTableConfig.getPid() != 0) {
                HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant2 = hrmSalaryTableConfigTenantMapper.selectById(hrmSalaryTableConfigTenant.getPid());
                HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant1 = hrmSalaryTableConfigTenantMapper.selectOne(new LambdaQueryWrapper<HrmSalaryTableConfigTenant>() {
                    {
                        eq(HrmSalaryTableConfigTenant::getTenantId, tenantId);
                        eq(HrmSalaryTableConfigTenant::getDistinguish, hrmSalaryTableConfigTenant2.getDistinguish());
                    }
                });
                pid = hrmSalaryTableConfigTenant1.getId();
            }
            hrmSalaryTableConfigTenant.setPid(pid);
            hrmSalaryTableConfigTenant.setTenantId(tenantId);
            hrmSalaryTableConfigTenantMapper.insert(hrmSalaryTableConfigTenant);
        });
        ContractPackage contractPackage = contractPackageMapper.selectOne(new LambdaQueryWrapper<ContractPackage>() {{
            last("limit 1");
        }});
        SystemConfig systemConfig = new SystemConfig();
        if (!ObjectUtils.isEmpty(contractPackage)) {
            systemConfig.setUsedNum(0L);
            systemConfig.setContractNum(contractPackage.getInitNum());
            systemConfig.setTenantId(tenantId.toString());
        } else {

            systemConfig.setUsedNum(0L);
            systemConfig.setContractNum(0L);
            systemConfig.setTenantId(tenantId.toString());
        }
        systemConfigMapper.insert(systemConfig);
        return 0;
    }


    /**
     * 计税规则列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<HrmSalaryTaxationRuleVo> selectTaxationRuleList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<HrmSalaryTaxationRuleVo> hrmSalaryTaxationRuleVos = new ArrayList<>();
        List<HrmSalaryTaxationRuleTenant> hrmSalaryTaxationRules = salaryTaxationRuleTenantMapper.selectList(null);
        hrmSalaryTaxationRules.forEach(HrmSalaryTaxationRuleTenant -> {
            HrmSalaryTaxationRuleVo hrmSalaryTaxationRuleVo = new HrmSalaryTaxationRuleVo();
            BeanUtils.copyProperties(HrmSalaryTaxationRuleTenant, hrmSalaryTaxationRuleVo);
            if (HrmSalaryTaxationRuleTenant.getIsTaxation() == 1) {
                hrmSalaryTaxationRuleVo.setBusiness("上年12月到今年11月(对应的工资发放方式为次月发上月工资)");

            } else if (HrmSalaryTaxationRuleTenant.getIsTaxation() == 2) {
                hrmSalaryTaxationRuleVo.setBusiness("今年1月到12月(对应的工资发放方式为当月发当月工资)");
            }
            if (HrmSalaryTaxationRuleTenant.getIsTaxation() == 0) {
                hrmSalaryTaxationRuleVo.setIsTaxation("否");
            } else {
                hrmSalaryTaxationRuleVo.setIsTaxation("是");
            }
            switch (HrmSalaryTaxationRuleTenant.getRoundUpId()) {
                case 2:
                    hrmSalaryTaxationRuleVo.setRoundUpId("保留两位小数");
                    break;
            }
            switch (HrmSalaryTaxationRuleTenant.getTaxationType()) {
                case 1:
                    hrmSalaryTaxationRuleVo.setTaxationType("工资薪金所得税");
                    break;
            }
            Long l = salaryPlanMapper.selectCount(new LambdaQueryWrapper<HrmSalaryPlan>() {{
                eq(HrmSalaryPlan::getTaxationId, HrmSalaryTaxationRuleTenant.getId());
            }});

            hrmSalaryTaxationRuleVo.setCount(l.intValue());

            hrmSalaryTaxationRuleVos.add(hrmSalaryTaxationRuleVo);

        });


        return new PageInfo<>(hrmSalaryTaxationRuleVos);
    }


    /**
     * 计税规则 的新增或修改
     *
     * @param HrmSalaryTaxationRuleTenant
     * @return
     */
    @Override
    public Integer insertOrUpdateTaxationRule(HrmSalaryTaxationRuleTenant HrmSalaryTaxationRuleTenant) {
        if (HrmSalaryTaxationRuleTenant.getId() != null) {
            salaryTaxationRuleTenantMapper.updateById(HrmSalaryTaxationRuleTenant);
        } else {
            salaryTaxationRuleTenantMapper.insert(HrmSalaryTaxationRuleTenant);
        }
        return 1;
    }


    /**
     * 计税规则下拉
     *
     * @return
     */
    @Override
    public List<JSONObject> taxationRuleBySelect() {
        List<JSONObject> jsonObjects = new ArrayList<>();
        List<HrmSalaryTaxationRuleTenant> hrmSalaryTaxationRules = salaryTaxationRuleTenantMapper.selectList(null);
        hrmSalaryTaxationRules.forEach(HrmSalaryTaxationRuleTenant -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", HrmSalaryTaxationRuleTenant.getId());
            jsonObject.put("name", HrmSalaryTaxationRuleTenant.getName());
            jsonObjects.add(jsonObject);

        });
        return jsonObjects;
    }


    @Override
    public HrmSalaryTaxationRuleTenant selectTaxationRuleById(Integer id) {
        HrmSalaryTaxationRuleTenant HrmSalaryTaxationRuleTenant = salaryTaxationRuleTenantMapper.selectById(id);
        return HrmSalaryTaxationRuleTenant;
    }


    @Override
    public Integer insertSalaryPlan(HrmSalaryPlanInsertBo hrmSalaryPlanInsertBo) {
        List<Integer> employeeIds = hrmSalaryPlanInsertBo.getEmployeeIds();
        if (ObjectUtils.isEmpty(employeeIds)) {
            throw new ResultException("适用员工不能为空");
        }
        List<HrmSalaryPlanEmployee> hrmSalaryPlanEmployees = hrmSalaryPlanEmployeeMapper.selectList(new LambdaQueryWrapper<HrmSalaryPlanEmployee>() {{
            in(HrmSalaryPlanEmployee::getEmployeeId, employeeIds);
        }});
        if (!ObjectUtils.isEmpty(hrmSalaryPlanEmployees)) {
            throw new ResultException("一位员工只能适用一个薪资组");
        }
        HrmSalaryPlan hrmSalaryPlan = new HrmSalaryPlan();
        BeanUtils.copyProperties(hrmSalaryPlanInsertBo, hrmSalaryPlan);
        hrmSalaryPlan.setUpdateTime(DateUtil.currentDateFormat());
        salaryPlanMapper.insert(hrmSalaryPlan);
        employeeIds.forEach(employeeId -> {
            HrmSalaryPlanEmployee salaryPlanEmployee = new HrmSalaryPlanEmployee();
            salaryPlanEmployee.setEmployeeId(employeeId);
            salaryPlanEmployee.setPlanId(hrmSalaryPlan.getId());
            salaryPlanEmployeeMapper.insert(salaryPlanEmployee);
        });
        return 1;
    }


    @Override
    public Integer updateSalaryPlan(HrmSalaryPlanInsertBo hrmSalaryPlanInsertBo) {
        List<Integer> employeeIds = hrmSalaryPlanInsertBo.getEmployeeIds();
        if (ObjectUtils.isEmpty(employeeIds)) {
            throw new ResultException("适用员工不能为空");
        }
        HrmSalaryPlan hrmSalaryPlan = new HrmSalaryPlan();
        BeanUtils.copyProperties(hrmSalaryPlanInsertBo, hrmSalaryPlan);
        hrmSalaryPlan.setUpdateTime(DateUtil.currentDateFormat());
        salaryPlanMapper.updateById(hrmSalaryPlan);
        salaryPlanEmployeeMapper.delete(new LambdaQueryWrapper<HrmSalaryPlanEmployee>() {{
            eq(HrmSalaryPlanEmployee::getPlanId, hrmSalaryPlan.getId());
        }});
        employeeIds.forEach(employeeId -> {
            HrmSalaryPlanEmployee salaryPlanEmployee = new HrmSalaryPlanEmployee();
            salaryPlanEmployee.setEmployeeId(employeeId);
            salaryPlanEmployee.setPlanId(hrmSalaryPlan.getId());
            salaryPlanEmployeeMapper.insert(salaryPlanEmployee);
        });
        return 1;
    }


    @Override
    public PageInfo<HrmSalaryPlanInsertVo> salaryPlanList(Integer pageNum, Integer pageSize) {
        List<HrmSalaryPlanInsertVo> hrmSalaryPlanInsertVos = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<HrmSalaryPlan> hrmSalaryPlans = salaryPlanMapper.selectList(null);
        hrmSalaryPlans.forEach(hrmSalaryPlan -> {
            HrmSalaryPlanInsertVo hrmSalaryPlanInsertVo = new HrmSalaryPlanInsertVo();
//            HrmSalaryTaxationRuleTenant HrmSalaryTaxationRuleTenant = salaryTaxationRuleTenantMapper.selectById(hrmSalaryPlan.getTaxationId());
            BeanUtils.copyProperties(hrmSalaryPlan, hrmSalaryPlanInsertVo);
//            hrmSalaryPlanInsertVo.setTaxationId(HrmSalaryTaxationRuleTenant.getName());
            switch (hrmSalaryPlan.getSalaryAdjustmentRules()) {
                case 1:
                    hrmSalaryPlanInsertVo.setSalaryAdjustmentRules("按转正日期/调薪生效日期前后的工资混合计算");
                    break;
                case 2:
                    hrmSalaryPlanInsertVo.setSalaryAdjustmentRules("整月按转正/调薪后的工资计算");
                    break;
                case 3:
                    hrmSalaryPlanInsertVo.setSalaryAdjustmentRules("15号前(含)转正/调薪整月按转正/调薪前计算，15号后转正/调薪整月按转正/调薪后计算");
                    break;
            }
            List<Integer> collect = salaryPlanEmployeeMapper.selectList(new LambdaQueryWrapper<HrmSalaryPlanEmployee>() {{
                eq(HrmSalaryPlanEmployee::getPlanId, hrmSalaryPlan.getId());
            }}).stream().map(HrmSalaryPlanEmployee::getEmployeeId).collect(Collectors.toList());

            List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
                in(EmployeeDO::getUserId, collect);
            }});
            hrmSalaryPlanInsertVo.setEmployeeNames(employeeDOS.stream().map(EmployeeDO::getName).collect(Collectors.toList()));
            hrmSalaryPlanInsertVos.add(hrmSalaryPlanInsertVo);
        });
        return new PageInfo<>(hrmSalaryPlanInsertVos);
    }


    @Override
    public HrmSalaryPlan selectSalaryPlanById(Integer id) {
        HrmSalaryPlan hrmSalaryPlan = salaryPlanMapper.selectById(id);
        List<Integer> collect = salaryPlanEmployeeMapper.selectList(new LambdaQueryWrapper<HrmSalaryPlanEmployee>() {{
            eq(HrmSalaryPlanEmployee::getPlanId, hrmSalaryPlan.getId());
        }}).stream().map(HrmSalaryPlanEmployee::getEmployeeId).collect(Collectors.toList());
        hrmSalaryPlan.setEmployeeIds(collect);
        return hrmSalaryPlan;
    }


    @Override
    public Integer deleted(Integer id) {
        salaryPlanEmployeeMapper.delete(new LambdaQueryWrapper<HrmSalaryPlanEmployee>() {{
            eq(HrmSalaryPlanEmployee::getPlanId, id);
        }});
        salaryPlanMapper.deleteById(id);
        return 1;
    }


}
